﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Plat.WfProcess.Common;

namespace Plat.WfProcess.Business
{
    /// <summary>
    /// 合并类型，获取下一步节点列表
    /// </summary>
    internal class NextActivityScheduleJoin : NextActivityScheduleBase
    {
        internal NextActivityScheduleJoin(string processGUID) :
            base(processGUID)
        {
            
        }

                /// <summary>
        /// 获取下一步节点列表
        /// </summary>
        /// <param name="processInstanceGUID"></param>
        /// <param name="fromTransition"></param>
        /// <param name="currentGatewayActivity"></param>
        /// <param name="conditionKeyValuePair"></param>
        /// <returns></returns>
        internal override NextActivityComponent GetNextActivityListFromGateway(string processInstanceGUID,
            TransitionEntity fromTransition,
            ActivityEntity currentGatewayActivity,
            ConditionKeyValuePair conditionKeyValuePair,
            out NextActivityMatchedType resultType)
        {
            NextActivityComponent child = null;
            NextActivityComponent gatewayComponent = null;
            resultType = NextActivityMatchedType.Unknown;
            TransitionManager tm = new TransitionManager(ProcessGUID);

            //直接取出下步列表，运行时再根据条件执行
            List<TransitionEntity> transitionList = tm.GetForwardTransitionList(currentGatewayActivity.ActivityGUID).ToList();
            foreach (TransitionEntity transition in transitionList)
            {
                child = GetNextActivityListFromGatewayCore(processInstanceGUID,
                    transition,
                    conditionKeyValuePair,
                    out resultType);

                gatewayComponent = AddChildToGatewayComponent(fromTransition, currentGatewayActivity, gatewayComponent, child);
            }

            return gatewayComponent;
        }

        #region 旧版本2010-04-13

        ///// <summary>
        ///// 获取下一步节点列表
        ///// </summary>
        ///// <param name="processInstanceGUID"></param>
        ///// <param name="fromTransition"></param>
        ///// <param name="currentGatewayActivity"></param>
        ///// <param name="conditionKeyValuePair"></param>
        ///// <returns></returns>
        //internal override NextActivityComponent GetNextActivityListFromGateway(string processInstanceGUID,
        //    TransitionEntity fromTransition,
        //    ActivityEntity currentGatewayActivity, 
        //    ConditionKeyValuePair conditionKeyValuePair,
        //    out NextActivityResultType resultType)
        //{
        //    NextActivityComponent child = null;
        //    NextActivityComponent gatewayComponent = null;
        //    resultType = NextActivityResultType.Unknown;
        //    TransitionManager tm = new TransitionManager(ProcessGUID);
        //    List<TransitionEntity> backwardTransitionList = tm.GetBackwardTransitionList(currentGatewayActivity.ActivityGUID).ToList();

        //    bool isJoinOk = false;
        //    if (currentGatewayActivity.GatewayDirectionType == GatewayDirectionEnum.AndJoin)
        //    {
        //        //所有连线上的条件必须全部满足，才能获取后续节点
        //        List<TransitionEntity> necTransitionList = tm.GetBackwardTransitionListNecessary(backwardTransitionList).ToList();
        //        foreach (TransitionEntity transition in necTransitionList)
        //        {
        //            bool isValid = tm.IsValidTransition(transition, conditionKeyValuePair);
        //            if (!isValid)
        //            {
        //                //退出循环
        //                resultType = NextActivityResultType.WaitingForOthersJoin;
        //                break;
        //            }
        //        }
        //        isJoinOk = true;
        //    }
        //    else if (currentGatewayActivity.GatewayDirectionType == GatewayDirectionEnum.OrJoin)
        //    {
        //        //获取满足条件定义的连线
        //        foreach (TransitionEntity transition in backwardTransitionList)
        //        {
        //            bool isValid = tm.IsValidTransition(transition, conditionKeyValuePair);
        //            if (isValid)
        //            {
        //                isJoinOk = true;
        //                //退出循环
        //                break;
        //            }
        //        }
        //        resultType = NextActivityResultType.NotMadeItselfToJoin;
        //    }
        //    else if (currentGatewayActivity.GatewayDirectionType == GatewayDirectionEnum.XOrJoin)
        //    {
        //        //按优先级获取第一个满足条件定义的连线
        //        backwardTransitionList.Sort(new TransitionPriorityCompare());
        //        foreach (TransitionEntity transition in backwardTransitionList)
        //        {
        //            bool isValid = tm.IsValidTransition(transition, conditionKeyValuePair);
        //            if (isValid)
        //            {
        //                isJoinOk = true;
        //                //退出循环
        //                break;
        //            }
        //        }
        //        resultType = NextActivityResultType.NotMadeItselfToJoin;
        //    }
        //    else if (currentGatewayActivity.GatewayDirectionType == GatewayDirectionEnum.ComplexJoin)
        //    {
        //        resultType = NextActivityResultType.Exceptional;
        //        throw new Exception("ComplexJoin 没有具体实现！");
        //    }
        //    else
        //    {
        //        resultType = NextActivityResultType.Exceptional;
        //        throw new Exception("Join 分支节点的类型不明确！");
        //    }

        //    //获得Join节点后续节点的列表
        //    if (isJoinOk)
        //    {
        //        child = GetNextActivityListFromGatewayCore(processInstanceGUID, 
        //            fromTransition, 
        //            conditionKeyValuePair, 
        //            out resultType);

        //        gatewayComponent = AddChildToGatewayComponent(fromTransition, 
        //            currentGatewayActivity, 
        //            gatewayComponent, 
        //            child);
        //    }
            
        //    return gatewayComponent;
        //}
        #endregion
    }
}
